var fs = require('fs')
var needle = require('needle');
var GridStore = require('../../entity').GridStore;
var DB = require('../../entity').DB;
var gm = require('gm').subClass({ imageMagick : true })

var options = {
    headers:{
        'Content-Type': 'image/jpeg'
    }
}

var url = 'http://wx.qlogo.cn/mmopen/Qkq3aoJq3TVGibibIxKuY2SxHCqNUOTsI7EakpCyADAsDNtqTHpLDejGhFGkDKHdANWohQX9VXYQiaNTunR1kOVlfmb6QPqD6ld/0';

(function(){
    /*fs.readFile('../../static/img/fj_default_headimg.jpg', {flag:'r'}, function(err, buffer){
        console.log(err)
        console.log(buffer)
    })*/
    needle.get(url,  function(err, resp, body) {
        console.log(err)
        console.log('----')
        console.log(body)
        // you can dump any response to a file, not only binaries.
        /*fs.writeFile('/home/lgyhitler/sss.jpg', body, function(data){
         gm('/home/lgyhitler/sss.jpg')
         .resize(64)
         .noProfile()
         .write('/home/lgyhitler/64.jpg', function (err) {
         if (!err)
         console.log('done');
         });
         })
        /*gm(body).resize(64).write('/home/lgyhitler/out.jpg', function (err) {
         if (err) return handle(err);
         console.log('Created an image from a Buffer!');
         });*/
        gm(body).resize(64).quality(80).toBuffer(function (err, buffer) {
            //TODO: buffer to mongodb
            console.log(buffer)
            /*var gridStore = new GridStore(DB, 'headlog_64.jpg', 'w', {
                content_type: 'image/jpg'
            });
            gridStore.open(function(err, gs){
                if(err){
                    return console.log(err);
                }
                gs.write(buffer, function(err, data){
                    if(err){
                        return console.log(err);
                    }
                    gs.close(function (err, result) {
                        console.log(result)
                    })
                })
            })*/
        })
    });
})()
